<template>
  <div class="login-page">
    <van-nav-bar title="账号登录" left-arrow @click-left="$router.go(-1)"/>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入用户名"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
      />
      <div style="margin: 25px">
        <van-button square block color="#21b97a" native-type="submit">登录</van-button>
      </div>
      <router-link to="/register" class="link">还没有账号，去注册~</router-link>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/uesr'
import { setToken, setStr } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await login(values)
      // console.log(res)
      setToken(res.body.token)
      this.$toast.success(res.description)
      this.$router.push('/my')
      setStr(JSON.stringify({ label: '上海', value: 'AREA|dbf46d32-7e76-1196', pinyin: 'shanghai', short: 'sh' }))
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-left: 20px;
  float: left;
}
.van-cell.van-field {
  margin: 20px 0px;
}
</style>
